$(document).ready(function(){
	$(".friendname").each(function(){
		var id = $(this).attr("id");
		if( "user"+id == $("#user"+id).attr("id")){
			$(this).remove();
		}

		$(this).bind("click",function(){
			setFriend(this);
			return false;
		});

		
	});
	
	$(".friendinput").focus(function(){  
			if("输入名字" == $(this).val()){$(this).val("")};}).blur(function(){ 
				if("" == $(this).val()){
					$(this).val("输入名字");
					$(".friendname").each(function(){
						$(this).css("display","block");
					});
				}else{
					var friendname = $(".friendinput").val();
					$(".friendname").each(function(){
						$(this).css("display","none");
						var text = $(this).text();
						if(text.indexOf(friendname) != -1){
							$(this).css("display", "block");
						}
					});
				}
	});

	var mytype = $("#user"+$("#myid").val()).next().text()*1;

	$(".type").each(function(){
		if($(this).text()*1 == 2){
			$(this).html(" 群主");
		}else if($(this).text()*1 == 1){
			if(2 == mytype){
				$(this).html('<a href="'+APP+'/group/deleteuser/gid/'+$("#groupid").val()+'/u/'+$(this).prev().attr("title")+'" class="deleteuser'+$(this).prev().attr("title")+'">删除</a> | <a href="'+APP+'/group/cancelmanager/gid/'+$("#groupid").val()+'/u/'+$(this).prev().attr("title")+'" class="cancelmanager'+$(this).prev().attr("title")+'">撤消管理员</a>');
				$(".deleteuser"+$(this).prev().attr("title")).bind("click",function(){deleteUser(this);return false;});
				$(".cancelmanager"+$(this).prev().attr("title")).bind("click",function(){cancelManager(this);return false;});
			}else{
				$(this).html(" 管理员");
			}
		}else{
			if(2 == mytype){
				$(this).html('<a href="'+APP+'/group/deleteuser/gid/'+$("#groupid").val()+'/u/'+$(this).prev().attr("title")+'" class="deleteuser'+$(this).prev().attr("title")+'">删除</a> | <a href="'+APP+'/group/setmanager/gid/'+$("#groupid").val()+'/u/'+$(this).prev().attr("title")+'" class="setmanager'+$(this).prev().attr("title")+'">升为管理员</a>');			
				$(".deleteuser"+$(this).prev().attr("title")).bind("click",function(){deleteUser(this);return false;});
				$(".setmanager"+$(this).prev().attr("title")).bind("click",function(){setManager(this);return false;});
			}else if(1 == mytype){
				$(this).html('<a href="'+APP+'/group/deleteuser/gid/'+$("#groupid").val()+'/u/'+$(this).prev().attr("title")+'" class="deleteuser'+$(this).prev().attr("title")+'">删除</a>');
				$(".deleteuser"+$(this).prev().attr("title")).bind("click",function(){deleteUser(this);return false;});
			}else{
				$(this).html(" 成员");
			}
		}	
		
	});

	$("#saveUserForm").submit( 
		function() {
			$("#invitesubmit").attr("disabled", "disabled");
			var  friends = "";
			$("input[name='friends[]']").each(function(i){
				if(0==i){
					friends += $(this).val();
				}else{$(this).val()
					friends += ","+ $(this).val();
				}
				$("#"+$(this).val()).remove();
			});
			
			if("" == friends){
				msg($("#groupid"),"没有选择任何好友");
				$("#invitesubmit").removeAttr("disabled");
			}else{
				//alert(friends);
				$.post($("#saveUserForm").attr("action"),{ajax:1,friends:friends,groupid:$("#groupid").val(),invitemsg:$("#invitemsg").val()},
					function(data){
						if("success"==data){
							$("#invitesubmit").removeAttr("disabled");
							msg($("#groupid"),"邀请成功");
							invitemsg:$("#invitemsg").val("");
							$("input[name='friends[]']").remove();
							$("#friendlist span").remove();
							var friendlist = $("#friendlist").html();
							$("#friendlist").html("");
							$("#inviteuser").html(friendlist);
						}else{
							msg($("#groupid"),"操作失败");
						}
						//alert(data);
					});
			}
			return false;
		} 
	);
	
});

function setFriend(obj){
	$("#friendlist").append('<li><input type="hidden" name="friends[]" value="'+$(obj).attr("id")+'"/> <a href="'+APP+'/user/index/u/'+$(obj).attr("id")+'">'+ $(obj).text() +' </a><span class="deletefriend'+$(obj).attr("id")+'"><a href="#">删除</a></span></li>');
	$(obj).css("display","none");
	$(".deletefriend"+$(obj).attr("id")).bind("click",function(){
		var id = $(this).prev().prev().val();
		$("#"+id).css("display","block");
		$(this).parent().remove();
	});
}

function deleteUser(obj){
	alertMessage(obj,"确定要删除此用户?");
	$(".msgOk").bind("click",function(){deleteUserAction(obj);});
	
}

function deleteUserAction(obj){
	$("#messageBox").remove();
	$.get($(obj).attr("href"),{ajax:1},function(data){
		if("success" == data){
			$(obj).parent().parent().fadeOut("slow",function(){ $(this).remove(); });
		}else{
			msg(obj,"操作失败");
		}
	});
}
function cancelManager(obj){
	alertMessage(obj,"确定撤消其管理员身份?");
	$(".msgOk").bind("click",function(){cancelManagerAction(obj);});
}

function cancelManagerAction(obj){
	var url = $(obj).attr("href");
	var url2 = url.replace("CancelManager","SetManager");
	var _class = $(obj).attr("class");
	var _class2 = _class.replace("cancelmanager","setmanager");
	$("#messageBox").remove();
	$.get(url,{ajax:1},function(data){
		if("success" == data){
			$(obj).attr("href",url2);
			$(obj).attr("class",_class2);
			$(obj).text("升为管理员");
			msg(obj,"设置成功");
			$("."+_class2).bind("click",function(){setManager(this);return false;});
		}else{
			msg(obj,"操作失败");
		}
	});
}
function setManager(obj){
	alertMessage(obj,"确定升其为管理员?");
	$(".msgOk").bind("click",function(){setManagerAction(obj);});
}
function setManagerAction(obj){
	var url = $(obj).attr("href");
	var url2 = url.replace("SetManager","CancelManager");
	var _class = $(obj).attr("class");
	var _class2 = _class.replace("setmanager","cancelmanager");
	$("#messageBox").remove();
	$.get(url,{ajax:1},function(data){
		if("success" == data){
			$(obj).attr("href",url2);
			$(obj).attr("class",_class2);
			$(obj).text("撤消管理员");
			msg(obj,"操作成功");
			$("."+_class2).bind("click",function(){cancelManager(this);return false;});
		}else{
			msg(obj,"操作失败");
		}
		
	});
}